from collections import deque
import mwindow


class JHProcess:
    def __init__(self):
        self.funcs = {}
        self.funcs['主界面'] = self.DoMain
        self.funcs['商店'] = self.DoShangDian
        self.funcs['菜单'] = self.DoCaiDan
        self.funcs['杂货店'] = self.DoZaHuoDian
        self.funcs['好友'] = self.DoHaoYou
        self.funcs['战利品'] = self.DoZhanLiPin
        self.funcs['广告'] = self.DoGuangGao
        self.funcs['任务'] = self.DoRenWu
        self.funcs['竞技场'] = self.DoJingJi
        self.funcs['挑战'] = self.DoTiaoZhan
        self.funcs['招募英雄'] = self.DoZhaoMuYingXiong
        self.funcs['招募确定'] = self.DoZhaoMuQueDing
        self.funcs['冒险'] = self.DoMaoXian
        self.funcs['隐秘圣所'] = self.DoYinMiShengSuo
        self.funcs['战斗场景'] = self.DoZhanDouChangJing
        self.funcs['战斗结果'] = self.DoZhanDouJieGuo
        self.funcs['选择增益效果'] = self.DoXuanzeZengYiXiaoGuo
        self.funcs['替换增益效果'] = self.DoTiHuanZengYiXiaoGuo
        self.funcs['隐秘圣所知道了'] = self.DoYinMiShengSuoZhiDaoLe
        self.triggers = deque([
            ('商店感叹号','商店'),
            ('金币感叹号','战利品'),
            ('菜单感叹号','菜单'),
            ('竞技满','竞技场'),
            ('竞技2','竞技场'),
            ('竞技1','竞技场'),
            ('招募感叹号','招募英雄'),
            ('冒险感叹号','冒险')
        ])
        #优先检查的窗口名，用于优化
        self.priorCheckWinNames = []
        # 隐秘圣所是否已打完
        self.YinMiShengSuo_Over = False
        # 是否处于隐秘圣所场景
        self.YinMiShengSuo_Scene = False
        None
    def OnTimer(self,mset:mwindow.MWindowSet,win:mwindow.MWindow):
        if win.name in self.funcs:
            self.funcs[win.name](win)
    def __MaybeNextWins(self,wins:list[str]):
        self.priorCheckWinNames = wins
    def __DoMainCheckClick(self,name:str,prior:str,posslist:dict,win: mwindow.MWindow):
        rtn = False
        if name in posslist:
            win.ClickLabel(name)
            self.__MaybeNextWins([prior])
            rtn = True
        return rtn
    def DoMain(self, win: mwindow.MWindow): #主界面
        posslist = win.ExistLabels(notReturnNone=True)
        for _ in range(len(self.triggers)):
            item = self.triggers.popleft()
            self.triggers.append(item)
            if(item[0] == '竞技1'):
                debug = 1
            if self.__DoMainCheckClick(item[0],item[1],posslist,win):
                return
    def DoShangDian(self, win: mwindow.MWindow): #商店
        win.ClickLabel("特惠礼包")
        win.ClickLabel("领取")
        win.ClickLabel("任意位置")
        win.ClickLabel("英雄礼包")
        win.ClickLabel("选择英雄")
        win.ClickLabel("任意位置")
        win.ClickLabel("关闭")
        win.ClickLabel("退出")
        self.__MaybeNextWins(['主界面'])
    def DoCaiDan(self, win: mwindow.MWindow): #菜单
        if win.ExistLabel('邮件感叹号'):
            win.ClickLabel("邮件感叹号")
            win.ClickLabel("邮件-领取全部")
            win.ClickLabel("邮件-任意位置")
            win.ClickLabel("邮件-关闭")
            # win.ClickLabel("退出")
            self.__MaybeNextWins(['主界面'])
        elif win.ExistLabel('杂货店感叹号'):
            win.ClickLabel("杂货店感叹号")
            self.__MaybeNextWins(['杂货店'])
        elif win.ExistLabel('好友感叹号'):
            win.ClickLabel("好友感叹号")
            self.__MaybeNextWins(['好友'])
        elif win.ExistLabel('任务感叹号'):
            win.ClickLabel("任务感叹号")
            self.__MaybeNextWins(['任务'])
        else:
            print('不存在感叹号')
            win.ClickLabel("退出")
            self.__MaybeNextWins(['主界面'])
    def DoZaHuoDian(self, win: mwindow.MWindow): #杂货店
        if win.ExistLabel('免费1'):
            win.ClickLabel("免费1")
            win.ClickLabel("免费按钮")
            win.ClickLabel("任意位置")
        if win.ExistLabel('免费2'):
            win.ClickLabel("免费2")
            win.ClickLabel("视频按钮")
        elif win.ExistLabel('免费3'):
            win.ClickLabel("免费3")
            win.ClickLabel("视频按钮")
        elif win.ExistLabel('免费4'):
            win.ClickLabel("免费4")
            win.ClickLabel("视频按钮")
        else:
            win.ClickLabel("关闭")
    def DoHaoYou(self, win: mwindow.MWindow): #好友
        win.ClickLabel("一键收送")
        win.ClickLabel("任意位置")
        win.ClickLabel("关闭")
        self.__MaybeNextWins(['主界面'])
    def DoGuangGao(self, win: mwindow.MWindow): #广告
        text = win.GetLabelText('已获得奖励')
        if text in ['获得奖励','已获得奖励','已获得奖状','已获得奖成']:
            win.ClickLabel("关闭")
            win.ClickLabel("任意位置")
    def DoRenWu(self, win: mwindow.MWindow): #任务
        poss = win.ExistLabel('宝箱')
        if poss != None:
            win.ClickRegion((poss.left,poss.top,poss.width,poss.height))
            win.ClickLabel("任意位置")
        if win.ExistLabel('领取'):
            win.ClickLabel("领取")
            win.ClickLabel("任意位置")
            self.__MaybeNextWins(['杂货店'])
        else:
            win.ClickLabel("关闭")
    def DoZhanLiPin(self, win: mwindow.MWindow): #战利品
            if win.ExistLabel('快速巡逻感叹号'):
                win.ClickLabel('快速巡逻')
                win.ClickLabel('免费领取')
            win.ClickLabel("领取全部")
            win.ClickLabel("任意位置")
            # win.ClickLabel("快速巡逻")
            self.__MaybeNextWins(['主界面'])
    def DoJingJi(self, win: mwindow.MWindow): #竞技
        if win.ExistLabel('周排名奖励'):
            win.ClickLabel("周排名奖励")
            win.ClickLabel("任意位置")
        if win.ExistLabel('日排名奖励'):
            win.ClickLabel("日排名奖励")
            win.ClickLabel("任意位置")
        if win.ExistLabel('数目0'):
            win.ClickLabel('退出')
            self.__MaybeNextWins(['主界面'])
        else:
            win.ClickLabel("挑战")
            self.__MaybeNextWins(['挑战'])
    def DoTiaoZhan(self, win: mwindow.MWindow): #挑战
        win.ClickLabel("碾压")
        win.ClickLabel("任意位置")
        self.__MaybeNextWins(['竞技场'])
    def DoZhaoMuYingXiong(self, win: mwindow.MWindow): #招募英雄
        win.ClickLabel("普通招募")
        if win.ExistLabel('免费领取'):
            win.ClickLabel("免费领取")
            win.ClickLabel("确定",interval=5)
        elif win.ExistLabel('冷却中'):
            win.ClickLabel("退出")
            self.__MaybeNextWins(['主界面'])
        elif win.ExistLabel('剩余次数0'):
            win.ClickLabel("退出")
            self.__MaybeNextWins(['主界面'])
        else:
            win.ClickLabel("免费领取")
            self.__MaybeNextWins(['广告'])
    def DoZhaoMuQueDing(self, win: mwindow.MWindow): #招募确定
        win.ClickLabel("确定")
    def DoMaoXian(self, win: mwindow.MWindow): #冒险
        if self.YinMiShengSuo_Over:
            win.ClickLabel("退出")
            self.__MaybeNextWins(['主界面'])
        else:
            win.ClickLabel("隐秘圣所")
            self.__MaybeNextWins(['隐秘圣所'])
    def DoYinMiShengSuo(self, win: mwindow.MWindow): #隐秘圣所
        self.YinMiShengSuo_Scene = True
        self.YinMiShengSuo_Over = (win.ExistLabel('鬼头') == None)
        poss = win.ExistLabel('光标')
        if self.YinMiShengSuo_Over or poss == None:
            print(f'隐秘圣所: 退出, over={self.YinMiShengSuo_Over}, poss={poss}')
            win.ClickLabel("退出")
            self.__MaybeNextWins(['主界面'])
            self.YinMiShengSuo_Scene = False
            return
        r = win.GetLabelRegion('光标')
        region = (poss.left,poss.top+poss.height,poss.width,poss.height)
        win.ClickRegion(region)
        win.ClickLabel("战斗")
        self.__MaybeNextWins(['战斗场景'])
    def DoZhanDouChangJing(self, win: mwindow.MWindow): #战斗场景
        if win.ExistLabel('开始战斗'):
            win.ClickLabel('卸下')
            win.ClickLabel('钩1')
            win.ClickLabel('钩2')
            win.ClickLabel('钩3')
            win.ClickLabel('钩4')
            win.ClickLabel('钩5')
            win.ClickLabel('开始战斗')
    def DoZhanDouJieGuo(self, win: mwindow.MWindow): #战斗结果
        if win.ExistLabel('确定'):
            win.ClickLabel('确定')
        elif win.ExistLabel('再次挑战'):
            if self.YinMiShengSuo_Scene:
                win.ClickLabel('再次挑战')
                self.__MaybeNextWins(['战斗场景'])
            else:
                win.ClickLabel('任意位置')

    def DoXuanzeZengYiXiaoGuo(self, win: mwindow.MWindow): #选择增益效果
        if win.ExistLabel('确定选择'):
            win.ClickLabel('技能2')
            win.ClickLabel('确定选择')
            self.__MaybeNextWins(['隐秘圣所'])
    def DoTiHuanZengYiXiaoGuo(self, win: mwindow.MWindow): #替换增益效果
        win.ClickLabel('跳过')
        self.__MaybeNextWins(['隐秘圣所'])
    def DoYinMiShengSuoZhiDaoLe(self, win: mwindow.MWindow): #隐秘圣所知道了
        win.ClickLabel('知道了')
        self.__MaybeNextWins(['隐秘圣所'])

